﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities.Excel.Xml;

namespace Common.Utilities.Excel.Export
{
    public abstract class Area
    {
        #region Property

        /// <summary>
        /// 区域的ID, 唯一标识
        /// </summary>
        public string ID { get; protected set; }

        /// <summary>
        /// 此区域使用的DataTable数据源
        /// </summary>
        public string DataTable { get; set; }

        /// <summary>
        /// 此区域对应上一区域间隔的行数, 如果此区域为工作表的第一个区域, 则此值为相对于工作表的行偏移量
        /// </summary>
        public int Row { get; protected set; }

        /// <summary>
        /// 此区域的对应工作表的列偏移量
        /// </summary>
        public int Column { get; protected set; }

        /// <summary>
        /// 此区域是否为重复区域
        /// </summary>
        public abstract bool IsRepeat { get; }

        /// <summary>
        /// 是否静态区域，静态区域不下移
        /// </summary>
        public bool IsStaticArea { get; protected set; }

        /// <summary>
        /// 此区域所属的Worksheet
        /// </summary>
        public Worksheet Worksheet { get; protected set; }

        /// <summary>
        /// 此区域所跨越的行数, 若此区域为StaticArea, 则此值需要从配置文件中获取, 若此区域为RepeatArea, 则此值从导出过程中由程序赋值
        /// </summary>
        public int RowSpan { get; set; }


        protected bool isComputeTopSpan = false;

        protected int _topRowSpan;

        /// <summary>
        /// 获取此区域距离工作表的起始行的间隔
        /// </summary>
        public int TopRowSpan 
        {
            get
            {
                if (!isComputeTopSpan)
                    _topRowSpan = GetTopRowSpan();
                return _topRowSpan;
            }
        }

        #endregion

        #region ctor

        /// <summary>
        /// 初始化属性
        /// </summary>
        /// <param name="areaNode"></param>
        /// <param name="worksheet"></param>
        public Area(XmlNode areaNode, Worksheet worksheet)
        {
            ID = XmlUtility.getNodeAttributeStringValue(areaNode, "id");
            DataTable = XmlUtility.getNodeAttributeStringValue(areaNode, "datatable");
            Row = XmlUtility.getNodeAttributeIntValue(areaNode, "row");
            Column = XmlUtility.getNodeAttributeIntValue(areaNode, "col");
            string staticArea = XmlUtility.getNodeAttributeStringValue(areaNode, "IsStaticArea");
            if(staticArea=="1")
            {
                IsStaticArea = true;
            }
            else
            {
                IsStaticArea = false;
            }
            Worksheet = worksheet;
        }
        #endregion

        #region Method
        /// <summary>
        /// 获取此区域距离工作表的起始行的间隔
        /// </summary>
        /// <returns></returns>
        protected int GetTopRowSpan()
        {
            // 先拿到Worksheet的起始行
            int num = Worksheet.Row;
            // 遍历这个区域前的所有区域, 把区域的起始行和区域跨越行数叠加
            foreach (Area area in Worksheet.Areas)
            {
                if (area.ID != ID)
                    num += area.Row + area.RowSpan;
                else
                    break;
            }
            return num;
        }

        #endregion

        /// <summary>
        /// 注入xml配置文件
        /// </summary>
        /// <param name="areaNode"></param>
        public virtual void SetXmlNode(XmlNode areaNode)
        {
            ID = XmlUtility.getNodeAttributeStringValue(areaNode, "id");
            DataTable = XmlUtility.getNodeAttributeStringValue(areaNode, "datatable");
            Row = XmlUtility.getNodeAttributeIntValue(areaNode, "row");
            Column = XmlUtility.getNodeAttributeIntValue(areaNode, "col");
        }

        /// <summary>
        /// 设置基本信息
        /// </summary>
        /// <param name="areaId">数据域的ID</param>
        /// <param name="tableName">数据域的数据表名</param>
        public void SetAreaBaseInfo(string areaId,string tableName)
        {
            ID = areaId;
            DataTable = tableName;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public virtual void InitArea()
        {

        }

    }
}
